<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="stylesheet" href="../style/journal.css" type="text/css" />
<style type="text/css"><!--
.googleadsense {
	margin: 2px;
	padding: 0px;
//--></style><script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-65008-1";
urchinTracker();
</script><title>给网页增加RSS</title>
</head>
<body>
<a href="index.html">Journal</a>(2005) | <a href="../blog/"><b>Blog</b></a>(2006) | <a href="http://www.fayland.org/cgi-bin/random_link.pl">RandomLink</a> | <a href="AboutFayland.html">WhoAmI</a> | <a href="LiveBookmark.html">LiveBookmark</a> | <a href="http://www.fayland.org/">HomePage</a>
<p><&lt;Previous: <a href="h2xs.html">使用h2xs打包模块</a>&nbsp;&nbsp;>>Next: <a href="POD.html">怎么写POD</a></p>
<h1>给网页增加RSS</h1>
<div class='content'>
<p>Category: <a href='Modules.html'>Modules</a> &nbsp; Keywords: <b>XML::RSS</b></p><h2>任务描述</h2>
RSS 的好处和用途不用多说，简而言之就是方便有用。详细的请<a href='http://www.google.com/search?hl=zh-CN&q=Rss+perl&lr='>Google it</a>.<br>
我的任务是给我自己的<a href='Eplanet.html'>Eplanet</a>，就是现在用的发布程序增加个RSS功能。<br>
详细的结果见：<a href='http://www.fayland.org/recent.rdf'>http://www.fayland.org/recent.rdf</a>

<h2>脚本编写</h2>
任务有两个，第一个是创建，第二个是更新。都可以用<a href='http://perl-rss.sourceforge.net/'>XML::RSS</a>来完成。<br>
创建需要填写Channel，而更新时不需要。对于image, textinput暂时略过。<br>
<h3>创建</h3>
Channel里有很多参数，如dc, syn, taxo等。很多我都不太懂。不过不懂就暂时先别加上，等懂了再加也不迟。<br>
<pre><code>
use XML::RSS;
my $RSS_file = "$Cfg{'htmldir'}/index.rdf";
my $rss = XML::RSS->new(version => '1.0');

print 'Create a new RSS file&lt;br>';
$rss->channel(
 &nbsp; &nbsp;title        => "Fayland's",
 &nbsp; &nbsp;link         => 'http://www.1313s.com/f/',
 &nbsp; &nbsp;description  => 'What Fayland says',
 &nbsp; &nbsp;language     => 'zh-cn',
 &nbsp; &nbsp;copyright    => '(c)2005, all rights reserved',
 &nbsp; &nbsp;syn => {
 &nbsp; &nbsp; &nbsp; &nbsp;updatePeriod     => 'daily',
 &nbsp; &nbsp; &nbsp; &nbsp;updateFrequency  => '1',
 &nbsp; &nbsp; &nbsp; &nbsp;updateBase       => '2005-01-12T00:00+00:00',
 &nbsp; &nbsp;},
);
# get 20 data from database, u can get more by set LIMIT
my $sth = $dbh->prepare(
    qq{SELECT * FROM cms ORDER BY cms_id DESC LIMIT 0, 20}
);
$sth->execute();
my $cats = $sth->fetchall_arrayref({});
$sth->finish;

foreach my $cat (@$cats) {
 &nbsp; &nbsp;$rss->add_item(
 &nbsp; &nbsp; &nbsp; &nbsp;title       => "$cat->{'cms_title'}",
 &nbsp; &nbsp; &nbsp; &nbsp;link        => "http://www.1313s.com/f/$cat->{'cms_file'}.html",
 &nbsp; &nbsp; &nbsp; &nbsp;description => "$cat->{'cms_describe'}",
 &nbsp; &nbsp;);
 &nbsp; &nbsp;print "Add $cat->{'cms_title'}&lt;br>";
}

$rss->save($RSS_file);
print 'all DOne!&lt;br>';
</code></pre>
<h3>更新</h3>
更新比创建还简单。就要先读取，然后再add_item, 再save就OK了。<br>
<pre><code>
use XML::RSS;
my $RSS_file = "$Cfg{'htmldir'}/index.rdf";
my $rss = XML::RSS->new(version => '1.0');

$rss->parsefile($RSS_file);

$rss->add_item(
 &nbsp; &nbsp;title       => "$tilte",
 &nbsp; &nbsp;link        => "http://www.1313s.com/f/$file.html",
 &nbsp; &nbsp;description => "$describe",
);
$rss->save($RSS_file);
</code></pre>
<h2>参考</h2>
<ul>
<li>请下载<a href='Eplanet.html'>Eplanet</a>后查看代码
<li><a href='http://perl-rss.sourceforge.net/'>XML::RSS</a>
<li><a href='http://www-106.ibm.com/developerworks/library/w-rss.html'>An introduction to RSS news feeds</a>
<li><a href='http://www.perl.com/pub/a/2000/01/rss.html'>RSS and You</a>
<li><a href='http://www.perl.com/pub/a/2001/11/15/creatingrss.html'>Create RSS channels from HTML news sites</a>
</ul></div>
<p><&lt;Previous: <a href="h2xs.html">使用h2xs打包模块</a>&nbsp;&nbsp;>>Next: <a href="POD.html">怎么写POD</a></p>
<p><strong>Options:</strong> <a href='http://del.icio.us/post?title=%E7%BB%99%E7%BD%91%E9%A1%B5%E5%A2%9E%E5%8A%A0RSS&url=http://www.fayland.org/journal/RSS.html'>+Del.icio.us</a></p>
<strong>Related items</strong>
<ul><li><a href='PerlTidy.html'>PerlTidy</a> < <span class='digit'>2004-10-30 19:23:26</span> ></li><li><a href='Highlight.html'>如何将Perl代码着色</a> < <span class='digit'>2004-11-20 22:13:22</span> ></li><li><a href='ask_tim.html'>Perl 6 什么时候才能完成？</a> < <span class='digit'>2004-12-30 18:42:06</span> ></li><li><a href='Perl6_ES14.html'>has $.Examples14 handles 'Perl6';</a> < <span class='digit'>2005-06-02 01:38:59</span> ></li><li><a href='050312.html'>给 eclipse 安装 EPIC plug-in</a> < <span class='digit'>2005-03-12 20:56:04</span> ></li><li><a href='050401.html'>Win32 下 Apache 和 #!/usr/bin/perl</a> < <span class='digit'>2005-04-01 10:10:59</span> ></li><li><a href='050519.html'>Synopsis localization</a> < <span class='digit'>2005-05-19 11:21:25</span> ></li><li><a href='Perl6_ES0.html'>@Examples[0] is Perl6</a> < <span class='digit'>2005-05-19 20:26:08</span> ></li><li><a href='Perl6_ES1.html'>given @Examples[1] when Perl6</a> < <span class='digit'>2005-05-19 22:05:56</span> ></li><li><a href='Perl6_ES2.html'>@Perl6 <== @Examples xx 2</a> < <span class='digit'>2005-05-20 18:53:54</span> ></li><li><a href='Perl6_ES3.html'>my Perl6 @Examples[3]</a> < <span class='digit'>2005-05-21 13:53:16</span> ></li><li><a href='Perl6_ES4.html'>say q:2 '@*Examples.[4] &Perl6()';</a> < <span class='digit'>2005-05-22 12:24:12</span> ></li><li><a href='Perl6_ES5.html'>sub Perl6 (*@Examples[5] is copy)</a> < <span class='digit'>2005-05-22 15:37:26</span> ></li><li><a href='Perl6_ES6.html'>multi sub Examples (6, Perl6) {...}</a> < <span class='digit'>2005-05-23 22:43:33</span> ></li><li><a href='Perl6_ES7.html'>sub infix:<(7)> ($Perl6, @Examples)</a> < <span class='digit'>2005-05-24 02:16:42</span> ></li><li><a href='Perl6_ES8.html'>let @Examples[8] = 'Perl6';</a> < <span class='digit'>2005-05-24 13:05:58</span> ></li><li><a href='050524.html'>The Shanghai PerlMonger meeting</a> < <span class='digit'>2005-05-24 20:41:25</span> ></li><li><a href='Perl6_ES9.html'>Perl6 | Examples & 9 ^ junction</a> < <span class='digit'>2005-05-26 12:35:29</span> ></li><li><a href='Perl6_ES10.html'>%Examples{'IO'} = open('Perl6');</a> < <span class='digit'>2005-05-30 00:24:01</span> ></li><li><a href='Perl6_ES11.html'>class Examples11 is Perl6 { ... }</a> < <span class='digit'>2005-05-30 13:31:31</span> ></li><li><a href='Perl6_ES12.html'>$Perl6 = $class.bless(:Example<12>);</a> < <span class='digit'>2005-05-31 11:38:46</span> ></li><li><a href='Perl6_ES13.html'>role Example13 { # Perl6 }</a> < <span class='digit'>2005-05-31 18:22:03</span> ></li><li><a href='Perl6_ES16.html'>my subtype Examples16 of Perl6 where /^kissu$/</a> < <span class='digit'>2005-06-04 23:59:29</span> ></li><li><a href='Perl6_ES17.html'>my enum Example17 <<:Perl(6) Today>></a> < <span class='digit'>2005-06-05 12:14:08</span> ></li><li><a href='Perl6_ES18.html'>my $queen; rule me {Perl6|Examples18}</a> < <span class='digit'>2005-06-06 12:26:32</span> ></li><li><a href='Perl6_ES19.html'>m:nth(19)/Perl6Examples/</a> < <span class='digit'>2005-06-06 22:02:01</span> ></li><li><a href='PBP3.html'>Tips from Perl Best Practices, Part 2</a> < <span class='digit'>2005-09-22 00:29:34</span> ></li><li><a href='Perl6_Rules.html'>用 Perl6::Rules 来写 Formatter</a> < <span class='digit'>2005-09-27 10:02:45</span> ></li><li><a href='mod_perl_learning.html'>a schedule of mod_perl learning</a> < <span class='digit'>2005-11-12 21:53:55</span> ></li><li><a href='mod_perl_configuring.html'>mod_perl 配置的一些指令</a> < <span class='digit'>2005-11-13 11:38:05</span> ></li><li><a href='modperl_helloworld.html'>modperl Apache2::HelloWorld</a> < <span class='digit'>2005-11-16 23:24:07</span> ></li><li><a href='pod2cn.html'>Perl 核心模块中文化</a> < <span class='digit'>2005-11-20 21:43:52</span> ></li><li><a href='modperl_http_cycle.html'>modperl 服务器的运行阶段和句柄</a> < <span class='digit'>2005-11-22 15:33:08</span> ></li><li><a href='modperl_PerlTransHandler.html'>modperl 的 PerlTransHandler 应用</a> < <span class='digit'>2005-11-23 22:24:02</span> ></li><li><a href='modperl_PerlAuthenHandler.html'>modperl 的用户验证</a> < <span class='digit'>2005-11-26 00:08:37</span> ></li><li><a href='modperl_Filter_part1.html'>modperl Filter Part1</a> < <span class='digit'>2005-12-18 14:47:29</span> ></li><li><a href='perldoc_ie.html'>用 IE 来浏览 perldoc</a> < <span class='digit'>2005-12-28 13:53:02</span> ></li></ul>
Created on <span class="digit">2005-01-12 18:22:53</span>, Last modified on <span class="digit">2005-05-21 00:14:09</span><br />
Copyright 2004-2005 All Rights Reserved. Powered by <a href="Eplanet.html">Eplanet</a> && <a href='http://catalyst.perl.org'>Catalyst</a> 5.62.
</body>
</html>